<?php


namespace app\common\service;


use AlibabaCloud\Client\AlibabaCloud;
use app\common\model\BaseModel;
use think\Cache;
use think\Log;

class AlibabaCloudService
{
    /**
     * 发送短信验证码
     */
    public static function sendSms($mobile, $templateCode, $params = '')
    {
        AlibabaCloud::accessKeyClient('LTAI4FvWDgwYwkyXF4kzJwtC','WvR3KHMuJAxKnv4XeSysJwuKN9I6w0')
            ->regionId('cn-hangzhou')->asDefaultClient();


        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $mobile,
                        'SignName' => "可语",
                        'TemplateCode' => $templateCode,
                        'TemplateParam' => $params,
                    ],
                ])
                ->request();
            $res = $result->toArray();
            if ($res['Code'] === 'OK' && $res['Message'] === 'OK') {
                return $res;
            } else {
                Log::write('【'.$res['Code'].'】'.$res['Message']);
            }
        } catch (ClientException $e) {
            Log::write('ClientException');
        } catch (ServerException $e) {
            Log::write('ServerException');
        }
    }

}